package main

import (
	"errors"
	"fmt"
)

/*
单向队列
*/

type SingleQueue struct {
	Front   int           //队列的头指针
	Rear    int           //队列的尾指针
	MaxSize int           //队列的最大容量
	Data    []interface{} //队列存放的数据区域
}

/**
构造SingleQueue
*/
func NewSingleQueue(maxsize int) *SingleQueue {
	singlequeue := new(SingleQueue)
	singlequeue.Front = -1
	singlequeue.Rear = -1
	singlequeue.MaxSize = maxsize
	singlequeue.Data = make([]interface{}, maxsize)
	return singlequeue
}

/**
显示队列中的元素
Front指向队头但是不包含队头
*/
func (this *SingleQueue) ShowQueue() {
	for i := this.Front + 1; i <= this.Rear; i++ {
		fmt.Println(this.Data[i])
	}
}

/**
入队
*/
func (this *SingleQueue) Enqueue(value interface{}) error {
	//判断是否队满
	//重要提示：Rear是队尾指针(是包含队尾元素的)
	if this.Rear == this.MaxSize-1 {
		return errors.New("queue full")
	}

	//移动队尾指针
	this.Rear++
	//插入数据
	this.Data[this.Rear] = value

	return nil
}

/**
出队
*/
func (this *SingleQueue) Dequeue() (interface{}, error) {
	//判断是否队空
	if this.Front == this.Rear {
		return nil, errors.New("queue empty")
	}
	//移动队头指针
	this.Front++

	//取出数据（）
	value := this.Data[this.Front]

	return value, nil
}

func main() {
	//queue := NewSingleQueue(3)
	//queue.Enqueue(1)
	//queue.Enqueue(2)
	//queue.Enqueue(3)
	//
	//queue.ShowQueue()
	//fmt.Println(queue.Dequeue())
	//fmt.Println(queue.Dequeue())
	//fmt.Println(queue.Dequeue())
	//fmt.Println(queue.Dequeue())

	rear := 3
	maxsize := 4
	fmt.Println((rear + 1) % maxsize)
}
